﻿using System;
using System.Collections.Generic;
using System.Text;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.IBatisRepository;
using OneMG.Phoenix.Respository;
using System.Data;
using OneMG.Phoenix.Entity;

namespace OneMG.Phoenix.Service
{
    public class AdminUserService : IAdminUserService
    {
		 IAdminUser adminUser = new AdminUserRepository();
		 
        public System.Int32 AddAdminUser(AdminUserModel model)
        {
			EAdminUser entity = ConvertModelToEntity(model);
			
            return adminUser.AddAdminUser(entity);
        }

		public void UpdateByUserID (AdminUserModel model)
		{
			EAdminUser entity = ConvertModelToEntity(model);

            adminUser.UpdateByUserID(entity);

		}

		public void DeleteByUserID (System.Int32 userID)
		{
		
            adminUser.DeleteByUserID(userID);

		}
		 
        public AdminUserModel GetByUserID (System.Int32 userID)
        {
        
			EAdminUser entity = adminUser.GetByUserID(userID);
			
            AdminUserModel model = ConvertEntityToModel(entity);

            string rightName = new RightsService().GetRightNameByUserID(userID);

            model.RightName = rightName;

            return model;
        }

        public IList<AdminUserModel> GetPagedList(int pageSize, int pageIndex, out int totalRecord)
        {
            IList<EAdminUser> entityList = adminUser.GetPagedList(pageSize, pageIndex, out totalRecord);

            if (entityList == null)
                return null;

            IList<AdminUserModel> modelList = new List<AdminUserModel>();

            foreach (var item in entityList)
            {
                AdminUserModel model = ConvertEntityToModel(item);
                if (model != null)
                    modelList.Add(model);
            }
            return modelList;


        }

        
		public AdminUserModel ConvertEntityToModel(EAdminUser entity)
		{
			if (entity == null)
            {
                return null;
            }
			AdminUserModel model = new AdminUserModel();
                        model.UserID = entity.UserID;
                        model.OrgID = entity.OrgID;
                        model.OrgIDPath = entity.OrgIDPath;
                        model.OrgNamePath = entity.OrgNamePath;
                        model.UserName = entity.UserName;
                        model.Password = entity.Password;
                        model.EMail = entity.EMail;
                        model.Telephone = entity.Telephone;
                        model.TrueName = entity.TrueName;
                        model.Memo = entity.Memo;
                        model.Visitation = entity.Visitation;
                        model.LastLoginTime = entity.LastLoginTime;
                        model.DataPermisType = entity.DataPermisType;
                        model.Status = entity.Status;
                        model.CreateTime = entity.CreateTime;
                        model.CreateUser = entity.CreateUser;
                        model.UpdateTime = entity.UpdateTime;
                        model.UpdateUser = entity.UpdateUser;
                        model.LockTime = entity.LockTime;
                        model.DeleteFlag = entity.DeleteFlag;
                        model.DeleteUser = entity.DeleteUser;
                        model.DeleteTime = entity.DeleteTime;
                        return model;
		}
		
		public EAdminUser ConvertModelToEntity(AdminUserModel model)
		{
			if (model == null)
            {
                return null;
            }
			EAdminUser entity = new EAdminUser();
                        entity.UserID = model.UserID;
                        entity.OrgID = model.OrgID;
                        entity.OrgIDPath = model.OrgIDPath;
                        entity.OrgNamePath = model.OrgNamePath;
                        entity.UserName = model.UserName;
                        entity.Password = model.Password;
                        entity.EMail = model.EMail;
                        entity.Telephone = model.Telephone;
                        entity.TrueName = model.TrueName;
                        entity.Memo = model.Memo;
                        entity.Visitation = model.Visitation;
                        entity.LastLoginTime = model.LastLoginTime;
                        entity.DataPermisType = model.DataPermisType;
                        entity.Status = model.Status;
                        entity.CreateTime = model.CreateTime;
                        entity.CreateUser = model.CreateUser;
                        entity.UpdateTime = model.UpdateTime;
                        entity.UpdateUser = model.UpdateUser;
                        entity.LockTime = model.LockTime;
                        entity.DeleteFlag = model.DeleteFlag;
                        entity.DeleteUser = model.DeleteUser;
                        entity.DeleteTime = model.DeleteTime;
                        return entity;
		}
    }
}